<!-- MENU-LOCATION=NONE -->
<!DOCTYPE HTML SYSTEM "-//Macromedia//DWExtension layout-engine 5.0//dialog">
<HTML>
<HEAD>
<!-- Copyright 2009, Alexei White. All rights reserved. -->
<Title>Run DreamUnit Tests</Title>
<link rel="stylesheet" type="text/css" href="../Shared/du/css/main.css" />
<script type="text/javascript" src="../Shared/du/js/head.js"></script>
<script type="text/javascript" src="../Shared/du/js/ws.js"></script>
<script type="text/javascript" src="../Shared/du/js/remoteData.js"></script>
<script type="text/javascript" src="../Shared/du/js/uid.js"></script>
<script type="text/javascript" src="../Shared/du/js/user.js"></script>
<script type="text/javascript" src="../Shared/du/js/sets.js"></script>
<script type="text/javascript" src="../Shared/du/js/tests.js"></script>
<script type="text/javascript" src="../Shared/du/js4/filetree.js"></script>
<script type="text/javascript" src="../Shared/du/js/prefs.js"></script>

<script type="text/javascript">
function windowDimensions() {
	return "800,610";
}
function commandButtons() { 
	//return new Array("Close", "window.close()"); 
	return new Array();
}

function init() {
	user.init();
	sets.init();
	prefs.init();
	homeScreen();
}
function homeScreen() {
	
	renderSetsList();
	var currentSet = prefs.getPref("CURRENTSET");
	if (!currentSet) {
		currentSet = sets.suggestDefaultSet();
		prefs.setPref("CURRENTSET", currentSet);
	}
	if (!currentSet || currentSet == "") {
		showNoSetsAvailableScreen();
	} else {
		// load the set
		renderSet(currentSet);

	}
}

function showSiteHelperScreen() {

	var bArea = document.getElementById("homeScreen");
	bArea.style.display = "none";
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";	
	var nArea = document.getElementById("noSetsScreen");
	nArea.style.display = "none";
	var tArea = document.getElementById("testResults");
	tArea.style.display = "none";		
	var sArea = document.getElementById("selectSiteInfo");
	sArea.style.display = "block";	
	var sHlper = document.getElementById("siteNamePh");
	sHlper.innerHTML = "Please Select the Site " + lastSiteName + " inside Dreamweaver";

}

function showNoSetsAvailableScreen() {

	var bArea = document.getElementById("homeScreen");
	bArea.style.display = "none";
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";	
	var sArea = document.getElementById("selectSiteInfo");
	sArea.style.display = "none";
	var tArea = document.getElementById("testResults");
	tArea.style.display = "none";		
	var nArea = document.getElementById("noSetsScreen");
	nArea.style.display = "block";	
}

function showLoading() {
	var nArea = document.getElementById("noSetsScreen");
	nArea.style.display = "none";	
	var bArea = document.getElementById("homeScreen");
	bArea.style.display = "none";
	var tArea = document.getElementById("testResults");
	tArea.style.display = "none";	
	var sArea = document.getElementById("selectSiteInfo");
	sArea.style.display = "none";	
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "block";	
}

function renderSetsList() {
	var res = "";
	for (var i = 0; i < sets.list.length; i++) {
		//res += "<button onClick=\"loadSet('" + sets.list[i].uid + "')\">" + sets.list[i].name + "</button><br>";
		if (sets.list[i].name) {
			res += "<input type=\"button\" class=\"maskedButton\" onClick=\"renderSet('" + sets.list[i].uid + "')\" value=\"" + sets.list[i].name.substr(0,20) + "\"><br>";
	}	
	}
	document.getElementById("setList").innerHTML = res;
}

function renderSet(uid) {

	var theSet = sets.getSetByUID(uid);
	tests.loadForSet(uid);
	var setArea = document.getElementById("homeScreen");
	var res = "";
	res += "<h1 style=\"margin:0px;padding:0px;\">" + theSet.name + "</h1>";
	res += "<p style=\"margin:0px;padding:0px;\">Created for " + theSet.sitename + "</p>";
	/*res += "Options: <input type=\"button\" class=\"maskedButton\" value=\"Delete Set\" onClick=\"deleteSet('" + uid + "')\" > ";
	res += "<input type=\"button\" class=\"maskedButton\" value=\"Update Set From Web\" onClick=\"updateSet('" + uid + "')\" > ";
	res += "<input type=\"button\" class=\"maskedButton\" value=\"&lt;- Back\" onClick=\"homeScreen()\" ><br>";*/
	res += "<mm:treecontrol name=\"testList\" id=\"testList\" style=\"height:300px;width:100%\">"  
	res += "<MM:TREECOLUMN name=\"TestName\" width=\"230\" state=\"visible\" value=\"Test Name\"><MM:TREECOLUMN name=\"Column2\" width=\"150\" state=\"visible\" value=\"Applies to page\"><MM:TREECOLUMN name=\"Column2\" width=\"150\" state=\"visible\" value=\"Result\"><MM:TREECOLUMN name=\"uid\" state=\"hidden\" width=\"1\">";
	res += "<mm:treenode icon=\"1\" value=\"Tests\" state=\"expanded\">";
	var rootfolder = site.siteRelativeToLocalPath("/");
	for (var i = 0; i < tests.list.length; i++) {
			var newfname = tests.list[i].appliesto.replace(rootfolder, "").replace(/:/g, "\\").replace("|", "").replace("|", "");	
			var newtname = tests.list[i].name.replace("|", "").replace("|", "");
			res += "<mm:treenode value=\"" + newtname + "|" + newfname + "|Not run yet|" + tests.list[i].uid + "\" state=\"expanded\"></mm:treenode>";
		}	
	res += "</mm:treenode>";
	res += "</mm:treecontrol>";
	
		//res += "<input type=\"button\" class=\"maskedButton\" value=\"Run Selected\" onClick=\"runSelectedTest()\" >  <input type=\"button\" class=\"maskedButton\" value=\"Run All Tests\" onClick=\"runAllTests()\" > ";
	res += "<input type=\"button\" class=\"maskedButton\" value=\"Run Selected\" onClick=\"runSelectedTest()\" > ";
	
	setArea.innerHTML = res;
	
	var sArea = document.getElementById("selectSiteInfo");
	sArea.style.display = "none";	
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";	
	var nArea = document.getElementById("noSetsScreen");
	nArea.style.display = "none";	
	var tArea = document.getElementById("testResults");
	tArea.style.display = "none";	
	var bArea = document.getElementById("homeScreen");
	bArea.style.display = "block";
}


function runSelectedTest() {
	try {
	var sIndex = document.getElementById("testList").selectedNodes[0].value;
	var uid = sIndex.split("|")[3];	
	var tIndex = tests.getTestIndexByUID(uid);
	var suid = 	tests.list[tIndex].suid;
	showLoading();
	setTimeout("runTest('" + uid + "');", 150);
	} catch(e) {
		alert("Please select a test.");
	}

}

var lastSiteName = "";
function runTest(uid) {
	var tIndex = tests.getTestIndexByUID(uid);
	var tObj = 	tests.list[tIndex];
	dreamweaver.setPrimaryView("code");
	var sObj = sets.getSetByUID(tObj.suid);
	site.setCurrentSite(sObj.sitename);
	lastSiteName = sObj.sitename;
	
	tests.implementTestOnFile(tObj.appliesto, tObj.js);
	
	//alert(site.getCurrentSite());
	//site.refresh("remote");
	site.refresh("local");
	var newLoc = utils.convertFileRefToURL(tObj.appliesto);
	site.setSelection([newLoc]);
	site.open();
	dw.closeDocument(dw.getDocumentDOM());
	site.open();	
	setTimeout("confirmPageIs('" + uid + "');", 500);
}

function confirmPageIs(tuid) {
	var tIndex = tests.getTestIndexByUID(tuid);
	var tObj = 	tests.list[tIndex];	
	var theDOM = dw.getDocumentDOM();
	var cDom = utils.convertFileRefToURL(tObj.appliesto).toLowerCase();
	var compareDOM = theDOM.URL.toString().replace("C|", "").replace("D|", "").replace("E|", "").toLowerCase();
	if (cDom.toString() != compareDOM) {
		//alert(cDom + "\n\n" + tObj.appliesto + "\n\n" + compareDOM);
		showSiteHelperScreen();
	} else {
		theDOM.setView("code");
		theDOM.setDesignViewMode("edtitable");
		theDOM.setView("design"); 
		theDOM.setDesignViewMode("live");
		theDOM.setView("design");
		setTimeout("extractResults('" + tuid + "');", 1500);
	}
}

function extractResults(tuid) {
	var theDOM = dw.getDocumentDOM();
	var brow = theDOM.browser.getWindow();
	if (brow) {
	var browDom = brow.document.body;
	if (browDom) {
	var ihtml = browDom.innerHTML;
	if (ihtml.indexOf("dreamunit_results")>-1) {
		tests.extractTestResults(tuid);
		setTimeout("recoverFromTest('" + tuid + "');", 100);
	} else {
		setTimeout("extractResults('" + tuid + "');", 500);
	}
	} else {
		setTimeout("extractResults('" + tuid + "');", 500);
	}
	} else {
		setTimeout("extractResults('" + tuid + "');", 500);
	}
}

function recoverFromTest(tuid) {
	var theDOM = dw.getDocumentDOM();
	//theDOM.setDesignViewMode("editable");
	//theDOM.setView("code");	
	//theDOM.synchronizeDocument();
	var tIndex = tests.getTestIndexByUID(tuid);
	var tObj = 	tests.list[tIndex];	
	tests.removeTestFromFile(tObj.appliesto);
	dw.closeDocument(dw.getDocumentDOM());
	//homeScreen();
	displayTestResults(tuid);
	//site.open();
}

var currentTestID = "";
function displayTestResults(tuid) {
	currentTestID = tuid;
	var sArea = document.getElementById("selectSiteInfo");
	sArea.style.display = "none";	
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";	
	var nArea = document.getElementById("noSetsScreen");
	nArea.style.display = "none";	
	var bArea = document.getElementById("homeScreen");
	bArea.style.display = "none";

	var tIndex = tests.getTestIndexByUID(tuid);
	var tObj = 	tests.list[tIndex];
	
	var res = "";
	
	// generate the test results for this test
	
	res += "<h1>Test Results For " + tObj.name + "</h1>";
	
	// generate a results tree
	res += "<mm:treecontrol name=\"testResultsTree\" id=\"testResultsTree\" style=\"height:200px;width:100%\">"  
	res += "<MM:TREECOLUMN name=\"MsgName\" width=\"230\" state=\"visible\" value=\"Message\"><MM:TREECOLUMN name=\"Column2\" width=\"150\" state=\"visible\" value=\"Pass or Fail\">";
	res += "<mm:treenode icon=\"1\" value=\"Results\" state=\"expanded\">";
	//tests.list[ti].results
	for (var i = 0; i < tObj.results.length-1; i++) {
			//alert(tObj.results[i]);
			var subset = tObj.results[i].split("**DWSPLIT**");
			var tName = subset[0];
			if (tName == "undefined")
				tName = "Test " + i;
			var result = "fail";
			if (subset[1] && subset[1] == "true")
				result = "pass";
			res += "<mm:treenode value=\"[" + i + "] " + tName.replace("\"", "").replace("\"", "") + "|" + result.replace("\"", "").replace("\"", "") + "\" state=\"expanded\"></mm:treenode>";
			
		}	
	res += "</mm:treenode>";
	res += "</mm:treecontrol><br>";
	res += "<input type=\"textarea\" id=\"testdetails\" style=\"width:100%; height:150px;\"><br>";
	res += " <input type=\"button\" class=\"maskedButton\" value=\"OK\" onClick=\"clearTimeout(showTestResultsTimer); homeScreen();\" >"	
	
	var tArea = document.getElementById("testResults");
	tArea.innerHTML = res;
	tArea.style.display = "block";	
	showTestResultsTimer = setTimeout("showResults()", 1000);
}

var showTestResultsTimer = null;

function showResults() {
	try {
		var sIndex = document.getElementById("testResultsTree").selectedNodes[0].value;
		var tIndex = tests.getTestIndexByUID(currentTestID);
		var tObj = 	tests.list[tIndex];
		var testNum = parseInt(sIndex.split("]")[0].replace("[", ""));
		var tInfo = tObj.results[testNum];
		//alert(tInfo);
		var tres = tInfo.split("**DWSPLIT**");
		var testResults = "";
		
		if (tres.length >2) {
			testResults = tres[2];
			if (testResults == "")
				testResults = tres[1];
		} else {

			testResults = tres[1];
		}
		

		document.getElementById("testdetails").value = testResults;
	} catch(e) {}
	showTestResultsTimer = setTimeout("showResults()", 300);
}

</script>

</HEAD>
<BODY onload="init()">
 <form name="uorl" style="margin:0px; padding:0px;">
  <table border="0" class="mainContentTable" width="100%">
    <!--DWLayoutTable-->
    <tr> 
      <td valign="top" nowrap>
		<table border="0"  width="100%"><tr><td><img src="../Shared/du/images/dulogo.png"/><br><td><td align="right" valign="bottom"><input type="button" onClick="window.close()" value="Close"><br>(v1.0) - www.dreamunittest.com</td></tr></table><br />
		<table width="100%" ><tr><td width="180" valign="top">CONFIGURED TEST SETS<br><div id="setList" style="margin-top:10px"></div></td><td><img src="../Shared/du/images/separator.gif"></td><td style="padding-left:10px;"  valign="top"><div id="homeScreen" style="display:none;">

		</div><div id="testResults" style="display:none;"></div><div id="waitScreen" style="display:none;">
			<h1 style="margin-top:130px;margin-left:25px;">Please wait..</h1>
		</div><div id="noSetsScreen" style="display:none;">
			<p style="margin-top:50px;">Sorry, no applicable sets have been configured for this site. Choose another site or configure a set for this one.</p>
			</div><div id="selectSiteInfo" style="display:none;">
				<h1 id="siteNamePh">Please Select the Site inside Dreamweaver</h1>
				<p>Even if the site <i>appears</i> to be selected right now, you will need to double-click on a file in the file explorer to formally &quot;select&quot; the site within Dreamweaver. Please close this window and do this now. Then you should be able to run tests normally.</p>
				</div>
		</td></tr></table>
      </td>
    </tr>
  </table>
</form>
</BODY>
</HTML>
